use crate::shared::date::Date;

use stdx::default::default;
use stdx::iter::IteratorExt;

use std::collections::HashMap;
use std::sync::LazyLock;

#[must_use]
pub fn time_info(time: &str) -> String {
    let Ok(date) = Date::parse_ymd(time) else {
        return String::new();
    };

    let iso_week = u32::from(date.iso_week());

    let dis_to_eoy = date.distance(&date.end_of_year());

    let lunar_soy_1 = LUNAR_TABLE.get(&date.year()).unwrap();
    let lunar_soy_2 = LUNAR_TABLE.get(&(date.year() + 1)).unwrap();

    let dis_to_lunar_soy = if date < *lunar_soy_1 {
        date.distance(lunar_soy_1) - 1
    } else {
        date.distance(lunar_soy_2) - 1
    };

    [iso_week, dis_to_eoy, dis_to_lunar_soy].iter().join_(",")
}

static LUNAR_TABLE: LazyLock<HashMap<u16, Date>> = LazyLock::new(|| {
    let mut ans: HashMap<u16, Date> = default();
    let file_content = include_str!(concat!(env!("OUT_DIR"), "/lunar11.txt"));
    for line in file_content.lines() {
        let (year, date) = line.split_once(':').unwrap();
        let date = Date::parse_ymd(date).unwrap();
        let year = year.parse().unwrap();
        ans.insert(year, date);
    }
    ans
});
